﻿using System.Linq;
using System.Xml.Linq;
using Framework.Common;

namespace System.Xml
{
    /// <summary>
    /// XmlNodeExtension
    /// </summary>
    public static class XmlNodeExtension
    {
        #region extension from XmlNode
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetStringAttribute(this XmlNode node, string key, string defaultValue = null)
        {
            XmlAttributeCollection attributes = node.Attributes;
            if (attributes != null && attributes[key] != null && !string.IsNullOrEmpty(attributes[key].Value))
                return attributes[key].Value;
            return defaultValue;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int GetIntAttribute(this XmlNode node, string key, int defaultValue = 0)
        {
            XmlAttributeCollection attributes = node.Attributes;
            int val = defaultValue;

            if (attributes != null && attributes[key] != null && !string.IsNullOrEmpty(attributes[key].Value))
            {
                int.TryParse(attributes[key].Value, out val);
            }
            return val;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static float GetFloatAttribute(this XmlNode node, string key, float defaultValue = 0)
        {
            XmlAttributeCollection attributes = node.Attributes;
            float val = defaultValue;

            if (attributes != null && attributes[key] != null && !string.IsNullOrEmpty(attributes[key].Value))
            {
                float.TryParse(attributes[key].Value, out val);
            }
            return val;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool GetBoolAttribute(this XmlNode node, string key, bool defaultValue = false)
        {
            XmlAttributeCollection attributes = node.Attributes;
            bool val = defaultValue;

            if (attributes != null && attributes[key] != null && !string.IsNullOrEmpty(attributes[key].Value))
            {
                bool.TryParse(attributes[key].Value, out val);
            }
            return val;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool GetBoolAttribute(this XElement node, string key, bool defaultValue = false)
        {
            var attr = node.Attribute(key);
            if (attr == null)
            {
                return defaultValue;
            }
            return attr.Value.StrToBoolean(defaultValue);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static float GetFloatAttribute(this XElement node, string key, float defaultValue = 0)
        {
            var attr = node.Attribute(key);
            if (attr == null)
            {
                return defaultValue;
            }
            return attr.Value.StrToFloat(defaultValue);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int GetIntAttribute(this XElement node, string key, int defaultValue = 0)
        {
            var attr = node.Attribute(key);
            if (attr == null)
            {
                return defaultValue;
            }
            return attr.Value.StrToInt(defaultValue);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetStringAttribute(this XElement node, string key, string defaultValue = null)
        {
            var attr = node.Attribute(key);
            if (attr == null)
            {
                return defaultValue;
            }
            return attr.Value;
        }
        #endregion

        #region extension from xml string
        /// <summary>
        /// get first node 's value from xml string
        /// </summary>
        /// <param name="xmlSource"></param>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public static string XmlFirstNode(this string xmlSource, string nodeName)
        {
            try
            {
                if (xmlSource.IsBlank()) return null;
                XDocument doc = XDocument.Parse(xmlSource);
                var result = doc.Descendants().FirstOrDefault(i => i.Name == nodeName);
                if (result != null)
                    return result.Value;
            }
            catch (Exception ex)
            {
                LogHelper.Fatal("XmlNodeExtension::XmlFirstNode::153", ex);
            }
            return null;
        }

        /// <summary>
        /// get first node 's attribute from xml string
        /// </summary>
        /// <param name="xmlSource"></param>
        /// <param name="nodeName"></param>
        /// <param name="attrName"></param>
        /// <returns></returns>
        public static string XmlFirstNode(this string xmlSource, string nodeName, string attrName)
        {
            try
            {
                if (xmlSource.IsBlank()) return null;
                XDocument doc = XDocument.Parse(xmlSource);
                var result = doc.Descendants().FirstOrDefault(i => i.Name == nodeName);
                if (result == null) return null;
                var attr = result.Attribute(attrName);
                if (attr == null) return null;
                return attr.Value;
            }
            catch (Exception ex)
            {
                LogHelper.Fatal("XmlNodeExtension::XmlFirstNode::187", ex);
                return null;
            }
        }
        #endregion
    }

}
